package gaoyang.biometricdemo;

import android.content.ComponentName;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.nestia.biometriclib.BiometricPromptManager;

public class RegisterActivity extends AppCompatActivity {

    private TextView identityget;
    private Button fingerget;
    private Button submit;
    private BiometricPromptManager mManager;

    //页面启动程序
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        identityget = findViewById(R.id.text_identity);
        fingerget = findViewById(R.id.btn_finger);
        submit = findViewById(R.id.btn_submit);

        //指纹按钮初始化状态-按钮可用；按钮不可用
        //Toast.makeText(RegisterActivity.this,"helloworld",Toast.LENGTH_LONG).show();
        mManager = BiometricPromptManager.from(this);
        if(!mManager.isHardwareDetected()){
            //设备不支持指纹硬件的时候，提示用户，并页面退出至主菜单页面
            fingerget.setText("此设备不支持指纹，用户无法注册");
            Toast.makeText(RegisterActivity.this, "此设备不支持指纹，用户无法注册", Toast.LENGTH_LONG).show();
            finish();
        }else if(!(mManager.isKeyguardSecure()||mManager.hasEnrolledFingerprints())){
            //手机并未开启指纹解锁，引导用户进入系统安全设置页面
            fingerget.setText("请先开启设备指纹解锁！");
            Toast.makeText(RegisterActivity.this, "请先开启手机指纹解锁，再返回APP重新注册", Toast.LENGTH_LONG).show();
            //华为系统指纹设置页面设备参数
            String pcgName = "com.android.settings";
            String clsName = "com.android.settings.fingerprint.FingerprintSettingsActivity";
            Intent intent = new Intent();
            ComponentName componentName = new ComponentName(pcgName, clsName);
            intent.setAction(Intent.ACTION_VIEW);
            intent.setComponent(componentName);
            startActivity(intent);
            //设置完成后，页面逻辑跳转至此活动页面。此时可关闭此注册活动，让用户重启进行注册
            finish();
        }else{
            fingerget.setText("指纹已录入");
        }


        //提交按钮响应程序
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                //检查身份证号码非空、格式正确性，18位数字
                String identity=identityget.getText().toString();
                if(identity.length()!=18)
                {
                    Toast.makeText(RegisterActivity.this, "身份证号码格式错误！修改后重新提交", Toast.LENGTH_LONG).show();
                    return;
                }

                //检查指纹录入状态，如果指纹没有录入
                if(!(fingerget.getText().toString().equals("指纹已录入"))){
                    Toast.makeText(RegisterActivity.this, "指纹未录入，请重新注册提交", Toast.LENGTH_LONG).show();
                    finish();
                }
                //用户注册数据打包发送给CM
                Toast.makeText(RegisterActivity.this, "身份证号码："+identityget.getText()+"注册成功", Toast.LENGTH_LONG).show();
                //用户注册成功或失败响应；成功时主菜单更新注册按钮，显示"用户已注册，设备已绑定"
                finish();
            }
        });
    }

}
